package com.example.xyh.serviceImpl;

import com.example.xyh.entity.OrderTable;
import com.example.xyh.service.OrderDetailService;
import com.example.xyh.service.OrderListService;
import com.example.xyh.service.OrderTableService;
import jakarta.annotation.Resource;
import lombok.extern.apachecommons.CommonsLog;
import org.modelmapper.ModelMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.List;

@Component
@CommonsLog
public class OrderDetailServiceImpl implements OrderDetailService {
    @Resource
    OrderTableService orderTableService;
    @Resource
    OrderListService orderListService;
    @Autowired
    protected ModelMapper modelMapper;

    @Override
    public List<com.example.xyh.dto.OrderDetailDto> findOrderDetailListByCustomerId(Integer customer_id) {
        List<OrderTable> orderTableList = orderTableService.findByCustomerId(customer_id);
        List<com.example.xyh.dto.OrderDetailDto> orderDetailDTOList = new ArrayList<>();
        for (OrderTable orderTable : orderTableList) {
            List<com.example.xyh.dto.OrderListDto> orderListDTOList = orderListService
                    .findByOrderId(orderTable.getOrderId());
            com.example.xyh.dto.OrderDetailDto orderDetailDTO = new com.example.xyh.dto.OrderDetailDto();
            modelMapper.map(orderTable, orderDetailDTO);
            orderDetailDTO.setOrderListDTOList(orderListDTOList);
            orderDetailDTOList.add(orderDetailDTO);
        }
        return orderDetailDTOList;
    }

    @Override
    public List<com.example.xyh.dto.OrderDetailDto> findOrderDetailListByShopId(Integer shop_id) {
        List<OrderTable> orderTableList = orderTableService.findByShopShopId(shop_id);
        List<com.example.xyh.dto.OrderDetailDto> orderDetailDTOList = new ArrayList<>();
        for (OrderTable orderTable : orderTableList) {
            List<com.example.xyh.dto.OrderListDto> orderListDTOList = orderListService.findByOrderId(orderTable.getOrderId());
            com.example.xyh.dto.OrderDetailDto orderDetailDTO = new com.example.xyh.dto.OrderDetailDto();
            modelMapper.map(orderTable, orderDetailDTO);
            orderDetailDTO.setOrderListDTOList(orderListDTOList);
            orderDetailDTOList.add(orderDetailDTO);
        }
        return orderDetailDTOList;
    }

    @Override
    public List<com.example.xyh.dto.OrderDetailDto> findByCustomerIdAndShopIdAndOrderState(Integer customer_id, Integer
            shop_id, Integer order_state) {
        List<OrderTable> orderTableList = orderTableService.findByCustomerIdAndShopShopIdAndOrderState(customer_id, shop_id, order_state);
        List<com.example.xyh.dto.OrderDetailDto> orderDetailDTOList = new ArrayList<>();
        for (OrderTable orderTable : orderTableList) {
            List<com.example.xyh.dto.OrderListDto> orderListDTOList = orderListService.findByOrderId(orderTable.getOrderId());
            com.example.xyh.dto.OrderDetailDto orderDetailDTO = new com.example.xyh.dto.OrderDetailDto();
            modelMapper.map(orderTable, orderDetailDTO);
            orderDetailDTO.setOrderListDTOList(orderListDTOList);
            orderDetailDTOList.add(orderDetailDTO);
        }
        return orderDetailDTOList;
    }
}
